package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.MessageEnum;
import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.error.CourseException;
import com.softer.wxzj.common.flow.enums.FlowStateEnum;
import com.softer.wxzj.common.flow.enums.FlowTypeEnum;
import com.softer.wxzj.common.flow.util.FlowAddAuditUtil;
import com.softer.wxzj.common.flow.util.FlowAuditUtil;
import com.softer.wxzj.common.flow.util.FlowUpdateAuditUtil;
import com.softer.wxzj.common.flow.util.Result;
import com.softer.wxzj.common.logic.DepReqLogic;
import com.softer.wxzj.common.util.*;
import com.softer.wxzj.entity.*;
import com.softer.wxzj.mapper.*;
import com.softer.wxzj.service.BaseBuildInfoService;
import com.softer.wxzj.service.OrgRegionService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;

/**
 * BaseBuildService class
 *
 * @author zcz
 * @date 2019/1/16
 */
@Slf4j
@Service
public class BaseBuildInfoServiceImpl implements BaseBuildInfoService {
    @Autowired
    private BaseBuildInfoMapper baseBuildInfoMapper;
    @Autowired
    private OperUtil operUtil;
    @Autowired
    private BaseSubRegionInfoMapper baseSubRegionInfoMapper;
    @Autowired
    BaseCellInfoMapper baseCellInfoMapper;
    @Autowired
    private ModifyUtil<BaseBuildInfo> modifyUtil;
    @Autowired
    private FlowUpdateAuditUtil flowUpdateAuditUtil;
    @Autowired
    private BaseModifyDetailServiceImpl baseModifyDetailService;
    @Autowired
    private FlowAuditUtil flowUtil;
    @Autowired
    private Result result;
    @Autowired
    private FlowAddAuditUtil flowAddAuditUtil;
    @Autowired
    BaseRegionInfoMapper baseRegionInfoMapper;
    @Autowired
    private BaseModifyReqMapper baseModifyReqMapper;
    @Autowired
    private OperUtil zoneCodeUtil;
    @Autowired
    FlowInstanceMapper flowInstanceMapper;
    @Autowired
    private ZoneCodeMapper zoneCodeMapper;
    @Autowired
    private CombineEntityUtil<BaseBuildInfo> combineEntityUtil;
    @Autowired
    private BaseHouseInfoMapper baseHouseInfoMapper;
    @Autowired
    private DicContentMapper dicContentMapper;
    @Autowired
    private BaseContractorBankInfoMapper baseContractorBankInfoMapper;
    @Autowired
    private BaseOrganizationMapper baseOrganizationMapper;
    @Autowired
    private BaseModifyDetailMapper baseModifyDetailMapper;
    @Autowired
    private SysCoreParamMapper sysCoreParamMapper;
    @Autowired
    private DepReqLogic depReqLogic;
    @Autowired
    private AccKeepAccountServiceImpl accKeepAccountServiceImpl;
    @Autowired
    private ExpProjectReqMapper expProjectReqMapper;
    @Autowired
    private FileServiceImpl fileService;
    @Autowired
    private SysOperatorMapper sysOperatorMapper;
    @Autowired
    private OrgRegionService orgRegionService;
    @Autowired
    private OrgBuildAuditMapper orgBuildAuditMapper;
    @Autowired
    private OrgRegionAuditMapper orgRegionAuditMapper;

    private int sortTypeTransform(String buildNo, String sortType) {
        String site = getHouseSite(buildNo);
        switch (sortType) {
            case "0":
                return transformCell(buildNo, site);
            case "1":
                return transformFloor(buildNo, site);
        }
        return 0;
    }

    private int transformCell(String buildNo, String site) {
        //类型
        List<BaseCellInfo> transform = baseCellInfoMapper.getCellTypeByBuildToTransform(buildNo);
        //所有房间
        List<BaseHouseInfo> list = baseHouseInfoMapper.getListByBuildNoToTransform(buildNo);
        //所有单元
        List<BaseCellInfo> listByBuildToTransform = baseCellInfoMapper.getAllListByBuildToTransform(buildNo);
        int maxFloor = baseHouseInfoMapper.getMaxFloorByBuildNo(buildNo);
        for (BaseCellInfo baseCellInfo : transform) {
            for (BaseCellInfo baseCellInfo1 : listByBuildToTransform) {
                for (int i = 1; i <= maxFloor; i++) {
                    int houseNo = 1;
                    for (BaseHouseInfo b : list) {
                        if (b.getHouseUse().equals(baseCellInfo.getCellType()) && i == Integer.parseInt(b.getFloorNo()) && baseCellInfo1.getId().equals(b.getCellNo())) {
                            String roomName = String.valueOf(i) + String.format("%02d", houseNo);
                            b.setRoomName(roomName);
                            b.setHouseSite(site + b.getCellName() + roomName);
                            houseNo++;
                        }
                    }
                }
            }
        }
        return baseHouseInfoMapper.updateList(list);
    }

    private int transformFloor(String buildNo, String site) {
        List<BaseCellInfo> transform = baseCellInfoMapper.getCellTypeByBuildToTransform(buildNo);
        List<BaseHouseInfo> list = baseHouseInfoMapper.getListByBuildNoToTransform(buildNo);
        int maxFloor = baseHouseInfoMapper.getMaxFloorByBuildNo(buildNo);
        for (BaseCellInfo baseCellInfo : transform) {
            for (int i = 1; i <= maxFloor; i++) {
                int houseNo = 1;
                for (BaseHouseInfo b : list) {
                    if (b.getHouseUse().equals(baseCellInfo.getCellType()) && i == Integer.parseInt(b.getFloorNo())) {
                        String roomName = String.valueOf(i) + String.format("%02d", houseNo);
                        b.setRoomName(roomName);
                        b.setHouseSite(site + b.getCellName() + roomName);
                        houseNo++;
                    }
                }
            }
        }
        return baseHouseInfoMapper.updateList(list);
    }

    private String getHouseSite(String buildNo) {
        BaseBuildInfo buildInfo = baseBuildInfoMapper.getInfo(buildNo);
        BaseRegionInfo regionInfo = baseRegionInfoMapper.getInfo(buildInfo.getRegionNo());
        BaseSubRegionInfo subRegionInfo = baseSubRegionInfoMapper.getInfo(buildInfo.getSubRegionNo());
        String zoneName = zoneCodeMapper.getZoneName(StringUtils.substring(regionInfo.getZoneCode(), 0, 6));
        String subRegionName = null == subRegionInfo || null == subRegionInfo.getSubRegionName() ? "" : subRegionInfo.getSubRegionName();
        //房屋坐落 区+小区名+ 分区名 +自然幢名+单元名+房间名
        String houseSite = zoneName + regionInfo.getRegionName() + subRegionName + buildInfo.getBuildName();
        return houseSite;
    }

    /**
     * 生成自然幢编号
     *
     * @return 自然幢编号
     */
    public String getBuildNo() {
        String regNo = baseBuildInfoMapper.selectMaxBuildNo();
        int regNum = 0;
        if (StringUtils.isBlank(regNo)) {
            regNo = NumStrEnum.ZERO.getNum();
            regNum = Integer.parseInt(regNo);
            String rNo = String.format("%07d", regNum);
            return NumStrEnum.TWO.getNum() + rNo;
        } else {
            regNum = Integer.parseInt(regNo);
            regNum++;
            return String.format("%07d", regNum);
        }
    }

    @Override
    public synchronized CommonResult insert(BaseBuildInfo baseBuildInfo) {

        baseBuildInfo.setBuildName(baseBuildInfo.getBuildName().replaceAll(" +", ""));

        int num = subAndRegionBybuildName(baseBuildInfo);
        if (num > 0) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "该自然幢名称已被使用，请换名称！");
        }
        String buildNo = getBuildNo();
        baseBuildInfo.setBuildNo(buildNo);
        //开工日期
        if (baseBuildInfo.getBuildDate().equals("")) {
            baseBuildInfo.setBuildDate(null);
        }
        //竣工日期
        if (baseBuildInfo.getFinishDate().equals("")) {
            baseBuildInfo.setFinishDate(null);
        }
        //预售许可日期
        if (baseBuildInfo.getPresellDate().equals("")) {
            baseBuildInfo.setPresellDate(null);
        }
        String id = UUIDUtil.getUUID();
        baseBuildInfo.setId(id);
        baseBuildInfo.setCreater(ShiroUserUtil.getUserName());
        String code = baseRegionInfoMapper.getInfo(baseBuildInfo.getRegionNo()).getZoneCode();
        baseBuildInfo.setZoneCode(code);
        //保存状态看添加人
        if (StringUtils.isEmpty(baseBuildInfo.getFlowState())) {
            baseBuildInfo.setFlowState(String.valueOf(NumberEnum.ZERO.getNum()));
            baseBuildInfo.setState(String.valueOf(NumberEnum.ONE.getNum()));
            //若是管理员添加
        } else {
            baseBuildInfo.setFlowState(String.valueOf(NumberEnum.TWO.getNum()));
            //把财务编号添加到数据库
            // addBuild(baseBuildInfo);
            baseBuildInfo.setState(String.valueOf(NumberEnum.ONE.getNum()));
            baseBuildInfo.setRecDate(DateUtils.getNow());
            flowUtil.saveDefaultFlowInstance(UUIDUtil.getUUID(), id, FlowTypeEnum.BASE_BUILD_INFO_REGISTER.getType(), null, null);
        }
        int i = baseBuildInfoMapper.insert(baseBuildInfo);
        //保存图片
        CommonResult commonResult = fileService.addFile(baseBuildInfo.getFileTypeList(), baseBuildInfo.getId());
        if (ShiroResultEnum.FAIL.getCode() == commonResult.getData()) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), commonResult.getMessage());
        }
        return CommonResult.idResult(i, id);
    }

    /**
     * 把财务编号添加到数据库
     */
    @Override
    public BaseBuildInfo addBuild(BaseBuildInfo baseBuildInfo) {
        //生成财务编号
        String financeCode = financeNo(baseBuildInfo);
        if (StringUtils.isBlank(financeCode)) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), "财务编号生成失败，请查找原因");
        } else {
            baseBuildInfo.setFinanceCode(financeCode);
        }
        return baseBuildInfo;
    }

    /**
     * 查询自然幢信息
     *
     * @param page
     * @param baseBuildInfo
     * @return
     */
    @Override
    public PageInfo<BaseBuildInfo> getBuildingReq(Page page, BaseBuildInfo baseBuildInfo) {
        String zoneCode = operUtil.getZoneCode();
        baseBuildInfo.setZoneCode(zoneCode);
        baseBuildInfo.setCreater(ShiroUserUtil.getUserName());
        List<BaseBuildInfo> list = baseBuildInfoMapper.getBuildingReq(baseBuildInfo);
        //查询楼层变更
        for (BaseBuildInfo b : list) {
            String typeNo = dicContentMapper.selectValueById(b.getBuildType());
            b.setBuildTypeNo(typeNo);

        }
        return delList(page, list);
    }

    @Override
    public BaseBuildInfo getInfoBuild(String id) {
        return baseBuildInfoMapper.getInfoBuild(id);
    }

    public PageInfo delList(Page page, List list) {
        Iterator<BaseBuildInfo> iterator = list.iterator();
        while (iterator.hasNext()) {
            BaseBuildInfo baseh = iterator.next();
            int count = expProjectReqMapper.selectByRegionId(baseh.getId());
            if (count > 0) {
                iterator.remove();
            }
        }
        PageInfo<BaseBuildInfo> pageInfo = new PageInfo<>();
        if (null != list && list.size() > 0) {
            List<BaseBuildInfo> b = baseModifyDetailService.pageAll(page, list);
            pageInfo = new PageInfo<>(b);
            pageInfo.setTotal(list.size());
        }

        return pageInfo;
    }

    /**
     * 财务接口生成一个财务编号
     */
    @Override
    public String financeNo(BaseBuildInfo baseBuildInfo) {
        String regionName;
        String sub = "";
        String firstLevelCode = sysCoreParamMapper.getParamValueByCode("58");
        regionName = baseRegionInfoMapper.getInfo(baseBuildInfo.getRegionNo()).getRegionName();
        if (StringUtils.isNotBlank(baseBuildInfo.getSubRegionName())) {
            sub = baseSubRegionInfoMapper.getInfo(baseBuildInfo.getSubRegionNo()).getSubRegionName();
        }
        //科目名称
        String codeName = regionName + sub + baseBuildInfo.getBuildName();
        return addInster(firstLevelCode, codeName);
    }

    /**
     * lianjie
     */
    public String addInster(String firstLevelCode, String codeName) {
        CloseableHttpClient client = HttpClients.createDefault();
        String url = "http://192.168.31.101:8091/AccVouch/getCode";
        //String url = "http://192.168.31.233:8081/baseBuildInfo/getInfo";
        HttpPost post = new HttpPost(url);
        //  对参数进行打包
        JSONObject jsonObject = new JSONObject();
        String secondLen = sysCoreParamMapper.getParamValueByCode("61");
        if (StringUtils.isBlank(secondLen)) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), "系统参数未设置财务编号位数");
        }
        jsonObject.accumulate("baseName", accKeepAccountServiceImpl.sqlName());
        jsonObject.accumulate("codeName", codeName);
        jsonObject.accumulate("secondLen", secondLen);
        jsonObject.accumulate("ccode", null);
        jsonObject.accumulate("firstLevelCode", firstLevelCode);
        post.setHeader("Content-Type", "application/json;charset=utf-8");
        post.addHeader("Authorization", "Basic YWRtaW46");
        //4.对打包好的参数，使用UrlEncodedFormEntity工具类生成实体的类型数据
        StringEntity entity = new StringEntity(jsonObject.toString(), "utf-8");
        //5.将含有请求参数的实体对象放到post请求中
        post.setEntity(entity);
        //6.新建一个响应对象来接收客户端执行post的结果
        try {
            CloseableHttpResponse response = client.execute(post);
            String s = EntityUtils.toString(response.getEntity());
            JSONObject jsonObjects = JSONObject.fromObject(s);
            return jsonObjects.getString("ccode");
        } catch (IOException e) {
            log.error("IOException错误信息:()", e.getMessage(), e);
        }
        return null;
    }

    /**
     * 修改房屋的地址
     *
     * @param info 旧值
     * @return
     */

    private int updateHouseSite(BaseBuildInfo b, BaseBuildInfo info) {
        BaseBuildInfo baseBuildInfo = baseBuildInfoMapper.getInfo(info.getId());
        //查询房屋有没有该小区若有则修改房屋的信息
        BaseRegionInfo baseRegionInfo = baseRegionInfoMapper.getInfo(baseBuildInfo.getRegionNo());
        int num = 0;
        String zoneName = zoneCodeMapper.getZoneName(StringUtils.substring(baseRegionInfo.getZoneCode(), 0, 6));
        List<BaseHouseInfo> house = baseHouseInfoMapper.selectAllByBuildId(baseBuildInfo.getId(), null);
        if (null != house) {
            if (!b.getBuildName().equals(info.getBuildName())) {
                String SubRegionName = "";
                if (StringUtils.isNotBlank(baseBuildInfo.getSubRegionNo())) {
                    BaseSubRegionInfo baseSubRegionInfo = baseSubRegionInfoMapper.getInfo(baseBuildInfo.getSubRegionNo());
                    SubRegionName = baseSubRegionInfo.getSubRegionName();
                }
                baseHouseInfoMapper.updateBankNo(baseBuildInfo.getId(), baseBuildInfo.getBankCode());
                for (BaseHouseInfo in : house) {
                    //修改自然幢
                    if (StringUtils.isNotBlank(in.getHouseSite())) {
                        BaseCellInfo baseCellInfo = baseCellInfoMapper.getInfo(in.getCellNo());
                        String houseSite = zoneName + baseRegionInfo.getRegionName() + SubRegionName +
                                b.getBuildName() + baseCellInfo.getCellName() + in.getRoomName();
                        num = baseHouseInfoMapper.updateHouseSite(houseSite, in.getId());
                        if (num == NumberEnum.ZERO.getNum()) {
                            throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
                        }
                    }
                }
            }
        }
        return num;
    }

    /**
     * 保存并提交
     */
    @Override
    public CommonResult insertSaveAndSubmit(String ids) {
        if (ids == null || ids.equals("")) {
            return new CommonResult(0, "请选择要提交的数据");
        }
        String[] ins = ids.split(",");
        List<String> lists = Arrays.asList(ins);
        for (String list : lists) {
            BaseBuildInfo baseBuildInfo = baseBuildInfoMapper.getInfo(list);
            int num = subAndRegionBybuildName(baseBuildInfo);
            if (num > 0) {
                return new CommonResult(ShiroResultEnum.FAIL.getCode(), baseBuildInfo.getBuildName() + "自然幢名称已被使用，请修改名称后提交！");
            }
        }
        // checkBuildName( buildName, String regionNo, String id);
        //调用增加方法
        int num = 0;
        String[] list = ids.split(",");
        //检查是否创建房间
        for (String id : list) {
            BaseBuildInfo baseBuildInfo = baseBuildInfoMapper.getInfo(id);
            int ret = baseHouseInfoMapper.selectHouse(id);
            if (baseBuildInfo == null || ret == 0) {
                return new CommonResult(0, "未维护完成" + baseBuildInfo.getBuildName() + "的信息");
            }
        }
        //检查房间是否维护
        for (String id : list) {
            List<BaseHouseInfo> houseInfos = baseHouseInfoMapper.getListByBuildNo(id);
            for (BaseHouseInfo houseInfo : houseInfos) {
                if (null == houseInfo.getStruArea()) {
                    throw new CourseException(ShiroResultEnum.FAIL.getCode(), "房屋信息不完整，请检查" + houseInfo.getHouseSite() + "房间信息！");
                }
            }
        }
        for (String id : list) {
            BaseBuildInfo baseBuildInfo = new BaseBuildInfo();
            baseBuildInfo.setId(id);
            baseBuildInfo.setRegionNo(baseBuildInfoMapper.getInfo(id).getRegionNo());
            baseBuildInfo.setSubRegionNo(baseBuildInfoMapper.getInfo(id).getSubRegionNo());
            //指定业务id和流程类型
            String initState = flowAddAuditUtil.initFlow(id, FlowTypeEnum.BASE_BUILD_INFO_REGISTER.getType(), null);
            //判断返回值状态   当为2时更新业务表
            if (StringUtils.isNotBlank(initState)) {
                baseBuildInfo.setFlowState(initState);
                baseBuildInfo.setState(NumStrEnum.ONE.getNum());
                num = baseBuildInfoMapper.update(baseBuildInfo);
            }
            if (num == 0) {
                return new CommonResult(ShiroResultEnum.FAIL.getCode(), "操作失败");
            }
        }
        return CommonResult.ResponseResult(num);
    }

    private String changeBuildType(BaseBuildInfo newBaseBuildInfo, BaseBuildInfo oldBaseBuildInfo) {
        //修改的房屋类型
        StringBuilder b = new StringBuilder(10);
        String typeNo = dicContentMapper.selectValueById(newBaseBuildInfo.getBuildType());
        String typeName = dicContentMapper.selectName(oldBaseBuildInfo.getBuildType());
        newBaseBuildInfo.setBuildTypeNo(typeNo);
        String oldTypeNo = dicContentMapper.selectValueById(oldBaseBuildInfo.getBuildType());
        oldBaseBuildInfo.setBuildTypeNo(oldTypeNo);
        if (!typeNo.equals(NumStrEnum.ZERO.getNum())) {
            if (oldTypeNo.equals(NumStrEnum.ZERO.getNum()) || Integer.parseInt(typeNo) > Integer.parseInt(oldTypeNo)) {
                //比较单元楼层  若大 则不能改
                int cellTypeNo = baseCellInfoMapper.selectMaxByCellFloor(newBaseBuildInfo.getId());
                if (Integer.parseInt(typeNo) < cellTypeNo) {
                    b.append("当前自然幢已维护了").append(cellTypeNo).append("层房屋，大于楼层属性").append(typeName).append("的最大楼层数（").append(oldTypeNo).append("），不能进行自然幢修改");
                    if (null == b) {
                        return null;
                    }
                }
            }
        }
        return b.toString();
    }

    /**
     * 保存到中间表
     *
     * @param newBaseBuildInfo
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult updateToSave(BaseBuildInfo newBaseBuildInfo) throws Exception {
        String modifyType = newBaseBuildInfo.getModifyType();
        String modifyCause = newBaseBuildInfo.getModifyCause();
        newBaseBuildInfo.setBuildName(newBaseBuildInfo.getBuildName().replaceAll(" ", ""));
        int num = subAndRegionBybuildName(newBaseBuildInfo);
        if (num > 0) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "该自然幢名称已被使用，请换名称！");
        }
        //获取原始业务数据
        BaseBuildInfo oldBaseBuildInfo = baseBuildInfoMapper.getInfoById(newBaseBuildInfo.getId());
        //查询楼层变更
        String news = changeBuildType(newBaseBuildInfo, oldBaseBuildInfo);
        if (StringUtils.isNotBlank(news)) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), news);
        }
        String id = newBaseBuildInfo.getId();
        //变更类型表id    注意区分id!!
        String uuid = UUIDUtil.getUUID();
        //保存变更表
        newBaseBuildInfo.setRegionName(oldBaseBuildInfo.getRegionName());
        newBaseBuildInfo.setRId(oldBaseBuildInfo.getRId());
        oldBaseBuildInfo.setRegionId(newBaseBuildInfo.getRegionId());
        newBaseBuildInfo.setRecDate(oldBaseBuildInfo.getRecDate());
        newBaseBuildInfo.setRId(oldBaseBuildInfo.getRId());
        newBaseBuildInfo.setSubRegionName(oldBaseBuildInfo.getSubRegionName());
        newBaseBuildInfo.setSubRegionFlag(oldBaseBuildInfo.getSubRegionFlag());
        oldBaseBuildInfo.setAddress(newBaseBuildInfo.getAddress());
        num = modifyUtil.updateToSave(oldBaseBuildInfo, newBaseBuildInfo, id, modifyType, modifyCause, uuid);
        // 图档变更操作
        int count = fileService.modifyFile(newBaseBuildInfo.getFileTypeList(), id, modifyType, modifyCause, uuid);
        if ((num + count) <= 0) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), "操作失败！请检查变更数据！");
        }
        //绑定审批流
        String initState = flowUpdateAuditUtil.initFlow(id, uuid, modifyType, null);
        //当返回通过时表明该记录不需要审核，直接更新到业务表
        if (FlowStateEnum.PASS.getState().equals(initState)) {
            //查询数据库原数据
            BaseBuildInfo info = baseBuildInfoMapper.getInfoById(id);
            BaseBuildInfo infoOld = new BaseBuildInfo();
            infoOld.setBuildName(info.getBuildName());
            infoOld.setId(info.getId());
            updateHouseSite(info, infoOld);
            //变更值合并到原数据   注意参数：uuid变更类型id   info数据库实体
            BaseBuildInfo completeEntity = modifyUtil.getCompleteEntity(uuid, info);
            //全表更新
            num = baseBuildInfoMapper.updateById(completeEntity);
            CommonResult result = fileService.dealPassModifyFile(uuid, id);
            if (ShiroResultEnum.FAIL.getCode() == result.getData()) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), result.getMessage());
            }
        }
        return CommonResult.ResponseResult(num);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResult update(BaseBuildInfo baseBuildInfo) {
        baseBuildInfo.setBuildName(baseBuildInfo.getBuildName().replaceAll(" ", ""));
        //若分区看该分区有没有重名的
        int num = subAndRegionBybuildName(baseBuildInfo);
        if (num > 0) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "该自然幢名称已被使用，请换名称！");
        }

        BaseBuildInfo info1 = baseBuildInfoMapper.getInfo(baseBuildInfo.getId());

        //查询原始数据
        BaseBuildInfo info = baseBuildInfoMapper.getInfoById(baseBuildInfo.getId());
        BaseBuildInfo infoOld = new BaseBuildInfo();
        infoOld.setBuildName(info.getBuildName());
        infoOld.setId(info.getId());
        infoOld.setBuildType(info.getBuildType());
        //查询楼层变更
        String news = changeBuildType(baseBuildInfo, infoOld);
        if (StringUtils.isNotBlank(news)) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), news);
        }
        //修改地址
        updateHouseSite(baseBuildInfo, infoOld);

        //合并实体  注意参数顺序 第一个前端传   第二个数据库原始数据
        BaseBuildInfo buildInfo = combineEntityUtil.combineEntity(baseBuildInfo, info);
        //注意是全表更新
        num = baseBuildInfoMapper.updateById(buildInfo);
        if (num > 0 && StringUtils.isNotEmpty(baseBuildInfo.getSortType()) && !info1.getSortType().equals(baseBuildInfo.getSortType())) {
            //sortTypeTransform(baseBuildInfo.getId(),baseBuildInfo.getSortType());
        }
        //处理图片修改
        CommonResult result = fileService.updateFile(buildInfo.getFileTypeList(), buildInfo.getId());
        if (ShiroResultEnum.FAIL.getCode() == result.getData()) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), result.getMessage());
        }
        return CommonResult.ResponseResult(num);
    }

    @Override
    public CommonResult delete(String id) {
        int num = baseHouseInfoMapper.selectHouseAndCellFromBuild(id);
        if (num != 0) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "该自然幢下存在单元或房屋，不允许删除！");

        }
        int count = flowInstanceMapper.getCountByBusinessId(id);
        if (count > 0) {
            return new CommonResult(ShiroResultEnum.FAIL.getCode(), "已存在审核中信息，不允许删除！");
        }
        //查询有没有变更
        count = baseModifyReqMapper.selectCountByBusinessId(id);
        if (count == 0) {
            //查询有没有单元
            count = baseCellInfoMapper.selectCountByBuildNo(id);
            if (count == 0) {
                count = baseBuildInfoMapper.delete(id);
            } else {
                count = baseBuildInfoMapper.updateState(id);
            }
        } else {
            count = baseBuildInfoMapper.updateState(id);
        }

        return CommonResult.ResponseResult(count);
    }

    @Override
    public PageInfo<BaseBuildInfo> getList(Page page, BaseBuildInfo baseBuildInfo) {
        //赋值区域,重要
        if (StringUtils.isEmpty(baseBuildInfo.getFlowState())) {
            baseBuildInfo.setFlowState("1,2,3");
        } else if (baseBuildInfo.getFlowState().equals("0,3")) {
            baseBuildInfo.setCreater(ShiroUserUtil.getUserName());
        }
        String zoneCode = operUtil.getZoneCode();
        baseBuildInfo.setZoneCode(zoneCode);
        List<BaseBuildInfo> lists;
        if (NumStrEnum.TWO.getNum().equals(baseBuildInfo.getNo())) {
            if (StringUtils.isBlank(baseBuildInfo.getFuzzyParam())) {
                lists = baseBuildInfoMapper.getList(baseBuildInfo);
                return delList(page, lists);
            } else if (NumStrEnum.THREE.getNum().equals(baseBuildInfo.getNo())) {
                lists = baseBuildInfoMapper.fuzzyGetList(baseBuildInfo.getFuzzyParam(), zoneCode, baseBuildInfo.getFlowState(), baseBuildInfo.getRegionNo(), baseBuildInfo.getCreater());
                for (BaseBuildInfo base : lists) {
                    int count = flowInstanceMapper.getCountByBusinessIdByType(base.getRegionId(), "region_info_modify");
                    base.setNum(count);
                }
                return delList(page, lists);
            } else {
                lists = baseBuildInfoMapper.fuzzyGetList(baseBuildInfo.getFuzzyParam(), zoneCode, baseBuildInfo.getFlowState(), baseBuildInfo.getRegionNo(), baseBuildInfo.getCreater());
                return delList(page, lists);
            }
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (StringUtils.isBlank(baseBuildInfo.getFuzzyParam())) {
            lists = baseBuildInfoMapper.getList(baseBuildInfo);
        } else {
            lists = baseBuildInfoMapper.fuzzyGetList(baseBuildInfo.getFuzzyParam(), zoneCode, baseBuildInfo.getFlowState(), baseBuildInfo.getRegionNo(), baseBuildInfo.getCreater());
        }
        for (BaseBuildInfo b : lists) {
            int no = baseCellInfoMapper.selectCountByBuildNo(b.getId());
            b.setNo(no + "");
        }
        //自然幢排序
        // List<BaseBuildInfo> list= reorder(lists);
        for (BaseBuildInfo buildInfo : lists) {
            if (StringUtils.isNotBlank(buildInfo.getSubRegionName())) {
                buildInfo.setBuildName(buildInfo.getBuildName() + "(" + buildInfo.getSubRegionName() + ")");
            }
            if (StringUtils.isNotBlank(baseBuildInfo.getZoneCode())) {
                String zoneName = zoneCodeMapper.getZoneName(baseBuildInfo.getZoneCode());
                buildInfo.setZoneCode(zoneName);
            }
            String typeNo = dicContentMapper.selectValueById(buildInfo.getBuildType());
            buildInfo.setBuildTypeNo(typeNo);
        }
        PageInfo<BaseBuildInfo> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    @Override
    public BaseBuildInfo getInfo(String id) {
        return null;
    }

    /**
     * 自然幢排序
     *
     * @param baseBuildInfos
     * @return
     */
    public List<BaseBuildInfo> reorder(List<BaseBuildInfo> baseBuildInfos) {
        for (BaseBuildInfo baseBuildInfo : baseBuildInfos) {
            int vLength = baseBuildInfo.getBuildName().length();
            //给默认值
            String vTep = "";
            //循环截取字符串
            for (int i = 0; i < vLength; i++) {
                String y = baseBuildInfo.getBuildName().substring(i, (i + 1));
                char[] a = y.toCharArray();
                if (a[0] > 47 && a[0] < 58) {
                    vTep = vTep.concat(y);
                }
            }
            if (StringUtils.isBlank(vTep)) {
                vTep = "100000";
            }
            int n;
            try {
                n = Integer.valueOf(vTep);
            } catch (Exception e) {
                n = 999999;
            }
            // int n=Integer.valueOf(vTep);
            baseBuildInfo.setNum(n);
        }
        baseBuildInfos.sort(Comparator.comparingInt(BaseBuildInfo::getNum));
        return baseBuildInfos;
    }

    @Override
    public BaseBuildInfo getInfoById(String id, String arcType) {
        BaseBuildInfo baseBuildInfo = baseBuildInfoMapper.getInfo(id);
        String houseState = NumStrEnum.ZERO.getNum();
        if (null != baseBuildInfo) {
            //查询是否有业主绑定过
            List<String> list = baseHouseInfoMapper.getIsOwnerListByBuildNo(id);
            if (null != list && list.size() > 0) {
                houseState = NumStrEnum.TWO.getNum();
                //查询是否有房间发生过缴存
                for (String b : list) {
                    String isDueState = depReqLogic.getIsDueState(b);
                    if (NumStrEnum.ONE.getNum().equals(isDueState)) {
                        houseState = NumStrEnum.THREE.getNum();
                        break;
                    }
                }
            } else {
                //查询是否维护过面积
                int count = baseHouseInfoMapper.getIsMaintainCountByBuildNo(id);
                houseState = count > 0 ? NumStrEnum.ONE.getNum() : houseState;
            }
        }
        baseBuildInfo.setHouseState(houseState);
        // 查询图档信息
        List<FileVO> fileVOList = fileService.getFileVOList(id, arcType);
        baseBuildInfo.setFileTypeList(fileVOList);
        return baseBuildInfo;
    }

    /**
     * 外网获取自然幢列表
     *
     * @param regionId 小区ID
     * @return 自然幢列表
     */
    @Override
    public PageInfo<BaseBuildInfo> getOuterNetBuildList(Page page, String regionId) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseBuildInfo> list = baseBuildInfoMapper.getOuterNetBuildList(regionId);
        return new PageInfo<>(list);
    }

    /**
     * 查询自然幢审核列表
     *
     * @param page
     * @param baseBuildInfo
     * @param flowType
     * @return
     */
    @Override
    public PageInfo<BaseBuildInfo> getAuditList(Page page, BaseBuildInfo baseBuildInfo, String flowType) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(flowType);
        List<BaseBuildInfo> list = new ArrayList<>();
        if (null != map) {
            //调用公用方法
            result.select(map, baseBuildInfo);
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseBuildInfoMapper.getListSubmit(baseBuildInfo);
            for (int i = 0; i < list.size(); i++) {
                String zone = zoneCodeMapper.getZoneName(list.get(i).getZoneCode());
                list.get(i).setZoneCode(zone);
            }
        }
        PageInfo<BaseBuildInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 添加流
     *
     * @param businessId
     * @param opinion
     * @param mark
     * @return
     */
    @Override
    public CommonResult addAudit(String businessId, String opinion, String mark) {
        int i = NumberEnum.ONE.getNum();
        String auditState = flowAddAuditUtil.audit(businessId, opinion, mark, null);
        BaseBuildInfo baseBuildInfo = new BaseBuildInfo();
        baseBuildInfo.setRegionNo(baseBuildInfoMapper.getInfo(businessId).getRegionNo());
        baseBuildInfo.setSubRegionNo(baseBuildInfoMapper.getInfo(businessId).getSubRegionNo());
        baseBuildInfo.setId(businessId);

        baseBuildInfo.setRecDate(DateUtils.getNow());

        //审核条件封装在一个类中
        result.result(auditState, baseBuildInfo);
        //不在审核中则更新业务表
        if (null != auditState && !FlowStateEnum.IN_AUDIT.getState().equals(auditState)) {
            if (FlowStateEnum.PASS.getState().equals(auditState)) {
                baseBuildInfo.setState(NumStrEnum.ONE.getNum());
//                //把财务编号添加到数据库
//                baseBuildInfo= addBuild(baseBuildInfo);
                //如果自然幢选择的物业企业，则进行区域绑定模块，进行解绑和绑定操作
                BaseBuildInfo info = baseBuildInfoMapper.getInfoById(businessId);
                dealBuildInfo(info);
            }
            i = baseBuildInfoMapper.update(baseBuildInfo);
        }
        return CommonResult.ResponseResult(i);
    }

    /**
     * 如果自然幢选择的物业企业，则进行区域绑定模块，进行解绑和绑定操作
     *
     * @param info
     */
    private void dealBuildInfo(BaseBuildInfo info) {
        if (StringUtils.isNotEmpty(info.getEnvironNo())) {
            String userName = ShiroUserUtil.getUserName();
            //如果物业公司不为空，将该自然幢之前绑定的物业公司进行解绑
            OrgBuildAudit orgBuildAudit = new OrgBuildAudit();
            orgBuildAudit.setModifier(userName);
            orgBuildAudit.setBuildId(info.getId());
            orgBuildAuditMapper.updateState(orgBuildAudit);
            //绑定
            OrgRegionAudit orgRegionAudit = new OrgRegionAudit();
            String uuid = UUIDUtil.getUUID();
            orgRegionAudit.setId(uuid);
            //查询主账号
            String operNo = sysOperatorMapper.getMainOperatorByCompanyId(info.getEnvironNo());
            orgRegionAudit.setOperNo(operNo);
            orgRegionAudit.setRegionId(info.getRegionId());
            orgRegionAudit.setFlowState(NumStrEnum.TWO.getNum());
            orgRegionAudit.setOpinion("自然幢审核通过后，自动绑定到该物业");
            orgRegionAudit.setCreater(userName);
            orgRegionAudit.setModifier(userName);
            orgRegionAuditMapper.add(orgRegionAudit);
            //绑定自然幢的审核记录
            orgBuildAudit.setOrgRegionAuditId(uuid);
            orgBuildAudit.setOperNo(operNo);
            orgBuildAudit.setRegionId(info.getRegionId());
            orgBuildAudit.setFlowState(NumStrEnum.TWO.getNum());
            orgBuildAudit.setState(NumStrEnum.ONE.getNum());
            orgBuildAudit.setCreater(userName);
            orgBuildAudit.setModifier(userName);
            orgBuildAuditMapper.add(orgBuildAudit);
        }
    }


    /**
     * 自然幢变更审核
     *
     * @param id      不是业务id
     * @param opinion 意见
     * @param mark    同意/拒绝
     * @return
     */
    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public CommonResult changeResidentialCommittee(String id, String opinion, String mark) {
        String bid = baseModifyReqMapper.getBusinessId(id);
        int num = NumberEnum.ZERO.getNum();
        String auditState = flowUpdateAuditUtil.audit(bid, opinion, mark);
        BaseModifyReq baseModifyReq = new BaseModifyReq();
        baseModifyReq.setId(id);
        baseModifyReq.setModifyCause(opinion);
        //终审
        if (null != auditState) {
            num = NumberEnum.ONE.getNum();
            //终审成功，修改业务表    其他状态不做处理
            if (FlowStateEnum.PASS.getState().equals(auditState)) {
                //查询数据库原数据
                BaseBuildInfo info = baseBuildInfoMapper.getInfoById(bid);
                BaseBuildInfo infoOld = new BaseBuildInfo();
                infoOld.setBuildName(info.getBuildName());
                infoOld.setId(info.getId());

                //变更值合并到原数据
                BaseBuildInfo completeEntity = modifyUtil.getCompleteEntity(id, info);
                //全表更新
                num = baseBuildInfoMapper.updateById(completeEntity);
                updateHouseSite(completeEntity, infoOld);
                dealBuildInfo(completeEntity);
                //图档更新
                CommonResult result = fileService.dealPassModifyFile(id, bid);
                if (ShiroResultEnum.FAIL.getCode() == result.getData()) {
                    throw new CourseException(ShiroResultEnum.FAIL.getCode(), result.getMessage());
                }
            }
        }
        return CommonResult.ResponseResult(num);
    }

    /**
     * 模糊查询自然幢信息列表
     *
     * @param page      分页实体
     * @param param     模糊查询参数
     * @param flowState 状态
     * @return 自然幢信息
     */
    @Override
    public PageInfo<BaseBuildInfo> fuzzySearch(Page page, String param, String flowState, String no) {
        String zoneCode = OperUtil.zoneCodeUtil.getZoneCode();
        //List<BaseBuildInfo> list= reorder(lists);
        List<BaseBuildInfo> lists = new ArrayList<>();
        if (no.equals(NumStrEnum.TWO.getNum())) {
            lists = baseBuildInfoMapper.fuzzySearch(zoneCode, param, flowState, no);
            for (BaseBuildInfo buildInfo : lists) {
                if (StringUtils.isNotBlank(buildInfo.getSubRegionName())) {
                    buildInfo.setBuildName(buildInfo.getBuildName() + "(" + buildInfo.getSubRegionName() + ")");
                }
                String typeNo = dicContentMapper.selectValueById(buildInfo.getBuildType());
                buildInfo.setBuildTypeNo(typeNo);

            }
            return delList(page, lists);

        } else {
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            lists = baseBuildInfoMapper.fuzzySearch(zoneCode, param, flowState, no);
            for (BaseBuildInfo buildInfo : lists) {
                if (no.equals(NumStrEnum.THREE.getNum())) {
                    int count = flowInstanceMapper.getCountByBusinessIdByType(buildInfo.getRegionId(), "region_info_modify");
                    buildInfo.setNum(count);
                } else if (no.equals(NumStrEnum.FOUR.getNum())) {
                    int count = flowInstanceMapper.getCountByBusinessIdByType(buildInfo.getId(), "base_cell_info_update");
                    buildInfo.setNum(count);
                }
                if (StringUtils.isNotBlank(buildInfo.getSubRegionName())) {
                    buildInfo.setBuildName(buildInfo.getBuildName() + "(" + buildInfo.getSubRegionName() + ")");
                }
            }
        }

        PageInfo<BaseBuildInfo> pageInfo = new PageInfo<>(lists);
        return pageInfo;
    }

    /**
     * 根据小区ID模糊查询自然幢信息列表
     *
     * @param page      分页实体
     * @param param     模糊查询参数
     * @param id        小区ID
     * @param flowState 状态
     * @return 自然幢信息
     */
    @Override
    public PageInfo<BaseBuildInfo> fuzzySearchByRegionId(Page page, String param, String id, String flowState) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseBuildInfo> list = baseBuildInfoMapper.fuzzySearchByRegionId(param, id, flowState);
        PageInfo<BaseBuildInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 没有分区时判断自然幢有没有重名
     *
     * @param buildName
     * @param regionNo
     * @param id
     * @return
     */
    @Override
    public CommonResult checkBuildName(String buildName, String regionNo, String id) {
        BaseRegionInfo bas = baseRegionInfoMapper.getInfo(regionNo);
        if (bas.getSubRegionFlag().equals(NumStrEnum.ONE.getNum())) {
            return CommonResult.strResult(NumberEnum.ONE.getNum(), null);
        }
        List<BaseBuildInfo> baseBuildInfos = baseBuildInfoMapper.getCountByBiuldNameAndRegionId(buildName, regionNo, id);
        int num = NumberEnum.ZERO.getNum();
        if (StringUtils.isNotBlank(id)) {
            for (int i = 0; i < baseBuildInfos.size(); i++) {
                if (baseBuildInfos.get(i).getId().equals(id)) {
                    baseBuildInfos.remove(i);
                }
            }
        }
        if (baseBuildInfos.size() > num) {
            return CommonResult.strResult(NumberEnum.ZERO.getNum(), "自然幢名称不能重复！");
        }
        return CommonResult.strResult(NumberEnum.ONE.getNum(), null);
    }

    /**
     * 业主销户的自然幢列表
     *
     * @param baseBuildInfo
     * @return
     */
    @Override
    public List<BaseBuildInfo> getListAndOwnerOut(BaseBuildInfo baseBuildInfo) {
        List<BaseBuildInfo> list;
        if (StringUtils.isBlank(baseBuildInfo.getFuzzyParam())) {
            list = baseBuildInfoMapper.getListAndOwnerOut(baseBuildInfo);
        } else {
            list = baseBuildInfoMapper.fuzzySearchByRegionNo(baseBuildInfo.getRegionNo(), baseBuildInfo.getFuzzyParam());
        }
        return list;
    }

    @Override
    public PageInfo<BaseBuildInfo> getListToBatchRef(Page page, BaseBuildInfo baseBuildInfo) {
        baseBuildInfo.setLoginZoneCode(zoneCodeUtil.getZoneCode());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseBuildInfo> list = baseBuildInfoMapper.getListToBatchRef(baseBuildInfo);
        PageInfo<BaseBuildInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 根据小区ID查询自然幢列表
     *
     * @param regionId    小区ID
     * @param subRegionId 分区ID
     * @return 自然幢列表
     */
    @Override
    public List<BaseBuildInfo> getBuildList(String regionId, String subRegionId) {
        return baseBuildInfoMapper.getBuildList(regionId, subRegionId);
    }

    /**
     * 查询变更详情
     *
     * @param page
     * @param id
     * @return
     */
    @Override
    public PageInfo<BaseModifyDetail> getModifyDetailList(Page page, String id) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseModifyDetail> list = baseBuildInfoMapper.getModifyDetailList(id);
        for (BaseModifyDetail modifyDetail : list) {
            switch (modifyDetail.getColumnName()) {
                case "bankCode":
                    String newBankCode = baseContractorBankInfoMapper.selectBankNameById(modifyDetail.getNewValue()).getBankName();
                    String oldBankCode = baseContractorBankInfoMapper.selectBankNameById(modifyDetail.getOldValue()).getBankName();
                    modifyDetail.setNewValue(null == newBankCode ? "" : newBankCode);
                    modifyDetail.setOldValue(null == oldBankCode ? "" : oldBankCode);
                    break;
                case "buildType":
                    String newBuildType = dicContentMapper.selectName(modifyDetail.getNewValue());
                    String oldBuildType = dicContentMapper.selectName(modifyDetail.getOldValue());
                    modifyDetail.setNewValue(null == newBuildType ? "" : newBuildType);
                    modifyDetail.setOldValue(null == oldBuildType ? "" : oldBuildType);
                    break;
                case "subRegionNo":
                    String newSubRegionNo = baseSubRegionInfoMapper.getInfo(modifyDetail.getNewValue()).getSubRegionName();
                    String oldSubRegionNo = baseSubRegionInfoMapper.getInfo(modifyDetail.getOldValue()).getSubRegionName();
                    modifyDetail.setNewValue(null == newSubRegionNo ? "" : newSubRegionNo);
                    modifyDetail.setOldValue(null == oldSubRegionNo ? "" : oldSubRegionNo);
                    break;
                case "buildAttr":
                    String newBuildAttr = dicContentMapper.selectName(modifyDetail.getNewValue());
                    String oldBuildAttr = dicContentMapper.selectName(modifyDetail.getOldValue());
                    modifyDetail.setNewValue(null == newBuildAttr ? "" : newBuildAttr);
                    modifyDetail.setOldValue(null == oldBuildAttr ? "" : oldBuildAttr);
                    break;
                case "developNo":
                    String[] ids = modifyDetail.getNewValue().split(",");
                    String[] idOld = modifyDetail.getOldValue().split(",");
                    modifyDetail.setNewValue("");
                    modifyDetail.setOldValue("");
                    String oldDep = "";
                    for (String newDep : ids) {
                        String newDevelopNo = baseOrganizationMapper.selectNameFromId(newDep);
                        String depName = null == newDevelopNo ? newDep : newDevelopNo;
                        modifyDetail.setNewValue(depName + "," + modifyDetail.getNewValue());
                    }
                    //，若最后一个字段为，则删除，
                    if (modifyDetail.getNewValue().endsWith(",")) {
                        String a = modifyDetail.getNewValue().substring(0, modifyDetail.getNewValue().length() - 1);
                        modifyDetail.setNewValue(a);
                    }
                    for (String idOlds : idOld) {
                        String oldDevelopNo = baseOrganizationMapper.selectNameFromId(idOlds);
                        oldDep = null == oldDevelopNo ? idOlds : oldDevelopNo;
                        modifyDetail.setOldValue(oldDep + "," + modifyDetail.getOldValue());
                    }
                    //，若最后一个字段为，则删除，
                    if (modifyDetail.getOldValue().endsWith(",")) {
                        String a = modifyDetail.getOldValue().substring(0, modifyDetail.getOldValue().length() - 1);
                        modifyDetail.setOldValue(a);
                    }
                    break;
                case "environNo":
                    String[] envie = modifyDetail.getNewValue().split(",");
                    String[] oldEnvir = modifyDetail.getOldValue().split(",");
                    modifyDetail.setNewValue("");
                    modifyDetail.setOldValue("");
                    for (String envirs : envie) {
                        String newEnvironNo = baseOrganizationMapper.selectNameFromId(envirs);
                        String en = null == newEnvironNo ? envirs : newEnvironNo;
                        modifyDetail.setNewValue(en + "," + modifyDetail.getNewValue());
                    }
                    if (modifyDetail.getNewValue().endsWith(",")) {
                        String a = modifyDetail.getNewValue().substring(0, modifyDetail.getNewValue().length() - 1);
                        modifyDetail.setNewValue(a);
                    }
                    for (String envirs : oldEnvir) {
                        String oldEnvironNo = baseOrganizationMapper.selectNameFromId(envirs);
                        String en = null == oldEnvironNo ? envirs : oldEnvironNo;
                        modifyDetail.setOldValue(en + "," + modifyDetail.getOldValue());
                    }
                    if (modifyDetail.getOldValue().endsWith(",")) {
                        String a = modifyDetail.getOldValue().substring(0, modifyDetail.getOldValue().length() - 1);
                        modifyDetail.setOldValue(a);
                    }
                    break;
                case "elevator":
                    if (StringUtils.isNotBlank(modifyDetail.getNewValue())) {
                        modifyDetail.setNewValue(NumStrEnum.ONE.getNum().equals(modifyDetail.getNewValue()) ? "是" : "否");
                    } else {
                        modifyDetail.setNewValue("");
                    }
                    if (StringUtils.isNotBlank(modifyDetail.getOldValue())) {
                        modifyDetail.setOldValue(NumStrEnum.ONE.getNum().equals(modifyDetail.getOldValue()) ? "是" : "否");
                    } else {
                        modifyDetail.setOldValue("");
                    }
                    break;
                case "sortType":
                    if (StringUtils.isNotBlank(modifyDetail.getNewValue())) {
                        modifyDetail.setNewValue(NumStrEnum.ONE.getNum().equals(modifyDetail.getNewValue()) ? "按层组合" : "按单元组合");
                    } else {
                        modifyDetail.setNewValue("");
                    }
                    if (StringUtils.isNotBlank(modifyDetail.getOldValue())) {
                        modifyDetail.setOldValue(NumStrEnum.ONE.getNum().equals(modifyDetail.getOldValue()) ? "按层组合" : "按单元组合");
                    } else {
                        modifyDetail.setOldValue("");
                    }
                    break;
            }
        }

        PageInfo<BaseModifyDetail> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询自然幢审核列表
     *
     * @param baseBuildInfo 查询实体
     * @return 待审核列表
     */
    @Override
    public PageInfo<BaseBuildInfo> fuzzyAuditList(Page page, BaseBuildInfo baseBuildInfo) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.BASE_BUILD_INFO_REGISTER.getType());
        List<BaseBuildInfo> list = new ArrayList<>();
        if (null != map) {
            //调用公用方法
            result.select(map, baseBuildInfo);
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseBuildInfoMapper.fuzzyAuditList(baseBuildInfo);
        }
        PageInfo<BaseBuildInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /***
     * t通过小区和分区编号看自然幢名称有没有重复的
     */
    public int subAndRegionBybuildName(BaseBuildInfo baseBuildInfo) {
        List<String> list = baseModifyDetailMapper.selectBuildName(baseBuildInfo.getRegionNo(), baseBuildInfo.getSubRegionNo());
        for (String s : list) {
            if (s.equals(baseBuildInfo.getBuildName())) {
                return 1;
            }
        }
        if (StringUtils.isBlank(baseBuildInfo.getSubRegionNo())) {
            checkBuildName(baseBuildInfo.getBuildName(), baseBuildInfo.getRegionNo(), baseBuildInfo.getId());
        }
        List<BaseBuildInfo> baseBuildInfos = baseBuildInfoMapper.getCountByBiuldNameAndRegionIdAndSub(baseBuildInfo.getBuildName(), baseBuildInfo.getRegionNo(), baseBuildInfo.getSubRegionNo());
        int num = NumberEnum.ZERO.getNum();
        if (StringUtils.isNotBlank(baseBuildInfo.getId())) {
            for (int i = 0; i < baseBuildInfos.size(); i++) {
                if (baseBuildInfos.get(i).getId().equals(baseBuildInfo.getId())) {
                    baseBuildInfos.remove(i);
                }
            }
        }
        return baseBuildInfos.size();
    }

}
